<?php

namespace App\Http\Models;

use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Support\Facades\Request;

class GoodsKill extends BaseModel
{
    protected $table = 'goods_kill';

    protected $guarded = [];

    //对应商品表
    public function goods()
    {
        return $this->belongsTo(Goods::class, 'gid');
    }

    //对应用户表
    public function user()
    {
        return $this->belongsTo('App\Http\Models\User', 'user_id', 'id');
    }

    //对应秒杀原因表
    public function KillRefuseReason()
    {
        return $this->hasOne('App\Http\Models\KillRefuseReason', 'kill_id', 'id');
    }

    /*
     * 设置关键字属性
     */
    public function setKeywordAttribute($keyword)
    {
        if (is_array($keyword)) {
            $keyword = implode(',', $keyword);
        } else {
            $keyword = str_replace('，', ',', $keyword);
        }
        $this->attributes['keyword'] = $keyword;
    }

    /*
     * 设置关键字属性
     */
    public function getKeywordAttribute($keyword)
    {
        $keyword = str_replace('，', ',', $keyword);
        return $keyword;
    }

    /*
     * 设置点评属性
     */
    public function setCommentAttribute($comment)
    {
        $comment = explode('&', $comment);
        $this->attributes['comment'] = json_encode($comment);

    }

    /*
     * 设置点评属性
     */
    public function getCommentAttribute($comment)
    {
        if ($comment) {
            $comment = json_decode($comment, true);
            return implode('&', $comment);
        }
        return $comment;
    }

    /**
     * @param $data
     * return bool
     */
    public function addKillGood($data)
    {
        return self::create($data);
    }


    public static function with($relations)
    {
        return new static;
    }

    public static function kill_field()
    {
        return [
            '09:00' => '9点场',
            '10:00' => '10点场',
            '11:00' => '11点场',
            '12:00' => '12点场',
            '14:00' => '14点场',
            '19:00' => '19点场',
            '20:00' => '20点场',
            '21:00' => '21点场',
        ];
    }

    public static function kill_field_info()
    {
        return [
            ['time' => '09:00', 'field' => '9点场'],
            ['time' => '10:00', 'field' => '10点场'],
            ['time' => '11:00', 'field' => '11点场'],
            ['time' => '12:00', 'field' => '12点场'],
            ['time' => '14:00', 'field' => '14点场'],
            ['time' => '19:00', 'field' => '19点场'],
            ['time' => '20:00', 'field' => '20点场'],
            ['time' => '21:00', 'field' => '21点场'],
        ];
    }

}